//
//  AskViewController.m
//  心水宝典
//
//  Created by zr on 2018/8/27.
//  Copyright © 2018年 zr. All rights reserved.
//

#import "AskViewController.h"
#import "AskHeaderView.h"
#import "AskTableViewCell.h"
#import "WeiteTieZiViewController.h"
#import "AskListModel.h"
#import "AskInfoViewController.h"

@interface AskViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, weak)UITableView * tableView;
@property (nonatomic, strong)NSMutableArray <AskListModel *> * modelArray;

@end

@implementation AskViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = WHITE_COLOR;
    self.modelArray = [NSMutableArray array];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeTabbar) name:@"changeTabbar" object:nil];
    
    [self loadData];
    [self setupNav];
    [self setupUI];
}


//- (void)setModelArray:(NSArray<AskListModel *> *)modelArray{
//
//    _modelArray = modelArray;
//
//    [self.tableView reloadData];
//}

- (void)loadData{
    
    [[NetworkTool sharedTools] GET:@"/api/circle?project_id=1&pid=12" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, NSDictionary *  _Nullable responseObject) {
        
        
//        NSString * msg = responseObject[@"msg"];
//        
//        if ([msg isEqualToString:@"无数据"]) {
//
//            NSArray * array = nil;
//
//            self.modelArray = array;
//        }else{
        
            NSArray *array = [NSArray yy_modelArrayWithClass:[AskListModel class] json:responseObject[@"data"]];
            [self.modelArray addObjectsFromArray:array];
            [self.tableView reloadData];
//        }
     
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
        NSLog(@"errror===%@",error);
    }];

}

- (void)setupNav{
   
    UIButton * backButton1 = [[UIButton alloc] init];
    
    [backButton1 setImage:ImageNamed(@"xie") forState:UIControlStateNormal];
    [backButton1 addTarget:self action:@selector(clickPushButton) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem * nextButton = [[UIBarButtonItem alloc] initWithCustomView:backButton1];
    
    self.navigationItem.rightBarButtonItem = nextButton;
    
}


- (void)setupUI{
    
    UITableView * tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREENH_HEIGHT) style:UITableViewStyleGrouped];
    tableView.delegate = self;
    tableView.dataSource = self;
    tableView.backgroundColor = [UIColor whiteColor];
    tableView.rowHeight = 255 * SCREENH6;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    self.tableView = tableView;
    
    [self.tableView registerClass:[AskTableViewCell class] forCellReuseIdentifier:@"AskTableViewCell"];
    [self.tableView registerClass:[AskHeaderView class] forHeaderFooterViewReuseIdentifier:@"AskHeaderView"];
    
    [self.view addSubview:self.tableView];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return 1;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    return self.modelArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    AskTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"AskTableViewCell" forIndexPath:indexPath];
    
    cell.model = self.modelArray[indexPath.row];
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 411 * SCREENH6;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    AskHeaderView * header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"AskHeaderView"];
    
    return header;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    HIDDEN_TAB;
    
    AskListModel * model = self.modelArray[indexPath.row];
    
    AskInfoViewController * infoVC = [AskInfoViewController new];
    
    infoVC.ID = model.id;
    
    [self.navigationController pushViewController:infoVC animated:YES];
    
    SHOW_TAB;
}


- (void)clickPushButton {
    
    HIDDEN_TAB;
    
    WeiteTieZiViewController * writeVC = [WeiteTieZiViewController new];
    
    [self.navigationController pushViewController:writeVC animated:YES];
    
    SHOW_TAB;
}

- (void)changeTabbar{
    
    self.tabBarController.selectedIndex = 1;
}


- (void)dealloc{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
